<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/all.conf.php');
require_once(__PHY_LIB.'/inc/page.user.inc.php');
require_once(__PHY_LIB.'/class/uWork.class.php');
require_once(__PHY_LIB.'/class/uCategory.class.php');

$rReq = validate($_REQUEST, 'action:e[list,edit,new,add,update,delete]');
$sMsg = false;


if ('add' == $rReq['action']) {
	do {
		$oWork = new uWork();
		$rP = validate($_POST, 'category_id:n|title:u|description:u|price:f|tags:u');
		$oTpl->assign('rP', $rP);
		if (!$rP['category_id']) { $sMsg .= "类别没有指定。"; $rReq['action'] = 'new'; break; }
		if (!$rP['title']) { $sMsg .= "没有标题。"; $rReq['action'] = 'new'; break; }
		if (!(0 <= $rP['price'])) { $sMsg .= "价格不规范。"; $rReq['action'] = 'new'; break; }
		foreach ($rP as $sField => $xValue) {
			$oWork->set($sField, $xValue);
		}
		$oWork->set('owner_id', $_The['User']['id']);
		$oWork->set('time_added', date('Y-m-d H:i:s'));
		if ($oWork->insert()) {
			$sMsg .= '添加成功。'; $rReq['action'] = 'list'; break;
		}
		else {
			$sMsg .= '添加失败。'; $rReq['action'] = 'list'; break;
		}
	} while (false);
}
elseif ('update' == $rReq['action']) {
	do {
		$oWork = new uWork($_REQUEST['id']);
		if (!(0 < $oWork->getID())) { $sMsg .= "该作品不存在。"; $rReq['action'] = 'list'; break; }
		if (!(0 < $oWork->getID())) { $sMsg .= "无权限修改。"; $rReq['action'] = 'list'; break; }
		$rP = validate($_POST, 'category_id:n|title:u|description:u|price:f|tags:u');
		$oTpl->assign('rP', $rP);
		if (!$rP['category_id']) { $sMsg .= "类别没有指定。"; $rReq['action'] = 'edit'; break; }
		if (!$rP['title']) { $sMsg .= "没有标题。"; $rReq['action'] = 'edit'; break; }
		if (!(0 <= $rP['price'])) { $sMsg .= "价格不规范。"; $rReq['action'] = 'edit'; break; }
		foreach ($rP as $sField => $xValue) {
			$oWork->set($sField, $xValue);
		}
		if ($oWork->update()) {
			$sMsg .= '修改成功。'; $rReq['action'] = 'list'; break;
		}
		else {
			$sMsg .= '修改失败。'; $rReq['action'] = 'list'; break;
		}
	} while (false);
}

if ('new' == $rReq['action']) {
}
elseif ('edit' == $rReq['action']) {
	$oWork = new uWork($_REQUEST['id']);
	$oTpl->assign('rWork', $oWork->getData());
}
else { // 'list' == $rReq['action']
	$rP = validate($_GET, 'category:n|pg:n');
	$oTpl->assign('rP', $rP);
	$sWhere = "owner_id='{$_The['User']['id']}'";
	/// 按类别查找 ///
	$oCtgry = new uCategory($rP['category']);
	if (0 < $oCtgry->getID()) {
		$sWhere .= " AND category_id IN ('".implode("','", $oCtgry->getTreeIdsByTop($oCtgry->getID()))."')";
	}
	/// 作品列表 ///
	$rPager = array(
		'Sze' => 5,
		'Req' => 'pg',
		'Num' => max(1, $rP['pg']),
		'Bse' => http_build_query(array_slice($rP, 0, -1)),
	);
	$oWorkList = new uWorkList();
	$oWorkList->cfgWhere($sWhere);
	$rPager['Cnt'] = $oWorkList->countAll();
	$rPager['Max'] = ceil($rPager['Cnt']/$rPager['Sze']);
	$oWorkList->load($rPager['Sze']*($rPager['Num'] - 1), $rPager['Sze']);
	while ($oWork = $oWorkList->nextItem()) {
		$oUser = new uUser($oWork->get('owner_id'), 'nick');
		$oWork->set('owner_nick', $oUser->get('nick'));
	}
	$oTpl->assign('rWorks', $oWorkList->fetchArray());
	$oTpl->assign('rPager', $rPager);
}

/// 类别目录列表 ///
$oCtgryList = new uCategoryList();
$oCtgryList->cfgOrder("tree");
$oCtgryList->load();
while ($oCtgry = $oCtgryList->nextItem()) {
	$sBreadCrumb = '';
	foreach ($oCtgry->fetchBreadCrumb() as $r) {
		$sBreadCrumb .= $r['title'].' > ';
	}
	$oCtgry->set('BreadCrumb', substr($sBreadCrumb, 0, -3));
}
$oTpl->assign('rSlctCtgrys', $oCtgryList->fetchArray());

$oTpl->assign('sMsg', $sMsg);
$oTpl->assign('sAction', $rReq['action']);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
